package it4s.service.api.factory;

import it4s.service.api.exception.ServiceAPIExeption;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;



public class TwitterFactoryInstance extends Factory{

	



	TwitterInstance twitterInstance;
	
	

	@Override
	public IServiceInstance createServiceInstance(IServiceInstance ser) {
		
		twitterInstance=new TwitterInstance();
		Properties prop = new Properties();
		String propFileName = "META-INF/twitter4j.properties";

		InputStream inputStream = getClass().getClassLoader()
				.getResourceAsStream(propFileName);

		
			try {
				prop.load(inputStream);
			} catch (IOException e) {
				throw new ServiceAPIExeption(new FileNotFoundException("Archivo '"
						+ propFileName + "' NO EXISTE"), "Error");
			}
		
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true)
		  .setOAuthConsumerKey(prop.getProperty("oauth.consumerKey"))
		  .setOAuthConsumerSecret(prop.getProperty("oauth.consumerSecret"))
		  .setOAuthAccessToken(prop.getProperty("oauth.accessToken"))
		  .setOAuthAccessTokenSecret(prop.getProperty("oauth.accessTokenSecret"));
		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitter = tf.getInstance();
		twitterInstance.setTwitter(twitter);
		return twitterInstance;
	}
}
